<template>
  <!-- Login 登录 -->
  <view class="container">
    <Header title="登录" :is-custom-back="isBackHome" @go-back="goBack" />
    <view class="login-box">
      <TImage
        :src="getLogo"
        width="200rpx"
        height="200rpx"
        mode="scaleToFill"
        background-color="#fff"
      />
      <view class="name">{{ getName }}</view>
    </view>
    <view class="btn-box">
      <!-- <view class="agreement">
        <image
          v-if="isAgreest"
          class="select-icon"
          src="https://hb-saas.obs.cn-south-1.myhuaweicloud.com/mall/saas/icon/card/selected-icon.png"
          mode="scaleToFill"
          @click="isAgreest = false"
        />
        <image
          v-else
          class="select-icon"
          src="https://hb-saas.obs.cn-south-1.myhuaweicloud.com/mall/saas/icon/card/select-icon.png"
          mode="scaleToFill"
          @click="isAgreest = true"
        />
        <text @click="navigateTo('userAgreement')">
          我已阅读并同意
          <text class="blue">《用户协议及隐私协议》</text>
        </text>
      </view> -->
      <!-- <button v-if="!showGetPhoneBtn" type="none" @click="login" class="login-btn">
        微信授权登录
      </button> -->
      <button
        type="none"
        open-type="getPhoneNumber"
        @getphonenumber="getPhoneNumber"
        class="login-btn"
      >
        授权登录
      </button>
    </view>
  </view>
</template>

<script lang="ts" setup>
  import Header from '@/components/Header/index.vue';
  import { navigateTo, onLoad, reLaunch, switchTab } from '@/router';
  import { useUserStoreWithOut } from '@/store/modules/user';
  import { onUnload } from '@dcloudio/uni-app';
  import { computed, ref, unref } from 'vue';
  import { TabbarPageEnum } from '@/enums/pageEnum';
  import { useAppStoreWithOut } from '@/store/modules/app';
  import TImage from '@/components/TImage/index.vue';
  import { Toast } from '@/utils';
  import { IsOfficialAccountEnum, ThridTypeEnum } from '@/enums/loginEnum';
  import { getWebAuthUrl } from '@/api/login';
  import { type PagesType } from '@/router/types/pages';
  /**  Login  登录 */

  const isAgreest = ref(true);

  const androidBack = ref(true);
  const { globalConfig } = useAppStoreWithOut();
  const useUser = useUserStoreWithOut();
  let __init: (() => void) | undefined;
  const isBackHome = ref<boolean>();
  const getLogo = computed(() => globalConfig?.logo);
  const getName = computed(() => globalConfig?.name);
  const reLaunchPage = ref<PagesType>();

  async function login() {
    // if (!isAgreest.value) return Toast('请先同意用户协议及隐私协议');
    // const code = await useUser.wxLogin();
    // const loginRes = await useUser.login(code);
    // androidBack.value = false;
    // loginRes.isBind ? success() : (showGetPhoneBtn.value = true);
  }

  async function getPhoneNumber({ detail: { code, iv, encryptedData } }: any) {
    if (!iv || !encryptedData) return;
    const loginCode = await useUser.wxLogin();
    // const userInfo = await useUser.login({
    //   code: loginCode,
    //   phoneCode: code,
    //   iv,
    //   encryptedData,
    //   thridType: ThridTypeEnum.WX3RD,
    // });

    // androidBack.value = false;
    // if (userInfo.isOfficialAccount === IsOfficialAccountEnum.UNKNOWN) {
    //   getWebAuthUrl().then((r) => {
    //     if (r) {
    //       uni.showLoading({ title: '登录中...', mask: true });
    //       navigateTo({
    //         name: 'webview',
    //         options: {
    //           url: r,
    //           title: '公众号授权中...',
    //           unLoadCallback: () => {
    //             uni.hideLoading();
    //             success();
    //           },
    //         },
    //       });
    //     } else {
    //       success();
    //     }
    //   });
    // } else {
    //   success();
    // }
  }

  function goBack() {
    androidBack.value = false;
    switchTab(TabbarPageEnum.BASE_HOME);
  }

  function success() {
    const routerName = unref(reLaunchPage) as any;
    if (routerName) {
      return routerName.includes('puppetTaabarPage')
        ? switchTab(routerName)
        : reLaunch({ name: routerName });
    }
    if (__init) {
      return uni.navigateBack({
        success: () => __init?.(),
      });
    }
    uni.$emit('regainPage');
  }

  onLoad<'Login'>(async (op) => {
    __init = op.__init;
    reLaunchPage.value = op.reLaunchPage;
    isBackHome.value = op.isBackHome;
  });

  onUnload(() => {
    if (androidBack.value && isBackHome.value) goBack();
  });
</script>

<style lang="scss" scoped>
  .container {
    height: 100vh;
    display: flex;
    align-items: center;
    // justify-content: center;
    padding-top: 200rpx;
    flex-direction: column;
    background-color: #fff;
    .login-box {
      margin: 0 auto;
      text-align: center;
      .name {
        color: #333;
        font-weight: bolder;
        font-size: 40rpx;
        text-align: center;
        margin-top: 20rpx;
      }
    }
    .btn-box {
      width: 100%;
      margin-top: 300rpx;
      .agreement {
        margin-bottom: 40rpx;
        font-size: 24rpx;
        color: #333;
        display: flex;
        align-items: center;
        justify-content: center;
        .select-icon {
          width: 32rpx;
          height: 32rpx;
          margin-right: 10rpx;
        }
        .blue {
          color: #2878ff;
        }
      }
      .login-btn {
        width: calc(100% - 60rpx);
        height: 96rpx;
        background-color: #e9e3b5;
        color: #333;
        font-size: 32rpx;
        border-radius: 20rpx;
        line-height: 96rpx;
        margin: 0 auto;
      }
    }
  }
</style>
